﻿using System.Collections;
using System.Windows.Controls;
using DataStore.Domain;
using Infrastructure.CurrentData;
using Infrastructure.ViewModels;
using Microsoft.Practices.Composite.Events;

namespace UI.Controls.Dictionaries
{
    /// <summary>
    /// Interaction logic for BindingGrid.xaml
    /// </summary>
    public partial class BindingGrid : UserControl, IDictionaryGrid
    {
        AllPublishingFormatsViewModel _allItems;
        readonly IEventAggregator _eventAggregator;

        /// <summary>
        /// Selection changed event
        /// </summary>
        public SelectionChangedEventHandler SelectionChanged;

        /// <summary>
        /// Called when [selection changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        protected void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SelectionChanged != null)
            {
                SelectionChanged(sender, e);
            }
        }

        public BindingGrid()
        {
            InitializeComponent();

            _eventAggregator = Current.Container.Resolve<IEventAggregator>();
            LoadItems();
        }

        /// <summary>
        /// Loads the publishers into the grid.
        /// </summary>
        void LoadItems()
        {
            _allItems = new AllPublishingFormatsViewModel(_eventAggregator);
            Items = _allItems.PublishingFormats;
            _allItems.Loaded += delegate
            {
                SetSelectedItem();
            };
        }


        #region Implementation of IDictionaryGrid

        /// <summary>
        /// Gets or sets the grid items.
        /// </summary>
        /// <value>The items.</value>
        public IEnumerable Items
        {
            get
            {
                return DataContext as IEnumerable;
            }
            set
            {
                DataContext = value;
                Bindings.ItemsSource = value;
            }
        }

        PublishingFormat _selected;
        /// <summary>
        /// Gets or sets the selected item.
        /// </summary>
        /// <value>The selected item.</value>
        public object Selected
        {
            get
            {
                return _selected;
            }
            set
            {
                _selected = value as PublishingFormat;
                SetSelectedItem();
            }
        }

        /// <summary>
        /// Set the selected item of the grid based on database identity
        /// </summary>
        void SetSelectedItem()
        {
            if (Selected != null)
            {
                foreach (PublishingFormat publisher in Bindings.Items)
                {

                    if (publisher.Id == _selected.Id)
                    {
                        Bindings.SelectedItem = publisher;
                    }
                }
            }
        }

        /// <summary>
        /// Updates the grid after edit operation.
        /// </summary>
        /// <param name="operation">The operation.</param>
        /// <param name="item">The item.</param>
        public void UpdateGrid(UpdateOperation operation, object item)
        {
            LoadItems();
        }

        /// <summary>
        /// Handles the SelectionChanged event of the Publishers control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        private void Bindings_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _selected = Bindings.SelectedItem as PublishingFormat;
            OnSelectionChanged(sender, e);
        }

        #endregion
    }
}
